//*************************************************************************************************
//************************  EE 400-401 Capstone Design Project - Group 15   ***********************
//*************************************************************************************************
//**
//**		Tabs:	This file looks best with tab stops set every 6 spaces.
//**
//*************************************************************************************************
//*************************************************************************************************
//**
//**          File:  LCD.asm
//**       Project:  XInC Library
//**       Created:  Jan.22.2008
//**       Revised:  None
//**
//**   Description:  Routines to initialize and write to LCD
//**
//** Compatibility:  Requires XInA v4.4 or higher.
//**
//**    Disclaimer:  You may incorporate this sample source code into your program(s) without
//**                 restriction.  This sample source code has been provided "AS IS" and the
//**                 responsibility for its operation is yours.  You are not permitted to
//**                 redistribute this sample source code as "Eleven sample source code" after
//**                 having made changes.  If you are going to re-distribute the source, we require
//**                 that you make it clear in the source that the code was descended from Eleven
//**                 sample source code, but that you have made changes.
//**
//*************************************************************************************************
//*************************************************************************************************
//**
//**	PIN CONFIGURATION::
//**	Port E: pins 0-7 correspond to D0-D7 on LCD
//**	Port I: pin 0 is LCD RS, pin 1 in LCD Enable Line 1, pin 2 in LCD enable Line 2
//**
//**	LCD_Init
//**	LCD_Command
//**	LCD_Char
//**
//*************************************************************************************************
//*************************************************************************************************

#ifndef __LCD_UTILS__
#define __LCD_UTILS__


//=============================================================================
// Input Params:	None
// Output Params:	None
//-----------------------------------------------------------------------------
// Description: 	Initializes LCD Screen. Cursor On, Blink On, Cursor Home.
// 			Screen is then ready and awaiting data input for display.
//=============================================================================



LCD_Init:

	st	r6, sp, 0		// Store r6 on stack
	add	sp, sp, 1		// Increment to avoid overwriting
	st	r0, sp, 0		// Store r0 on stack
	add	sp, sp, 1		// Increment to avoid overwriting


	jsr 	r6, DelayReallyLong	// Delay until VDD reaches 4.5V (20ms)

	mov 	r0, 0xFF00		// Configure Pins on board as outputs
	outp 	r0, GPCcfg
	mov 	r0, 0x0700
	outp 	r0, GPIcfg

	mov 	r0, 0x38		// Function set command
	jsr 	r6, LCD_Command

	jsr 	r6, DelayLong

	mov 	r0, 0x38		// Function set command
	jsr 	r6, LCD_Command

	jsr 	r6, DelayLong

	mov 	r0, 0x38		// Function set command
	jsr 	r6, LCD_Command

	jsr 	r6, DelayLong

	mov 	r0, 0x38		// Function set command
	jsr 	r6, LCD_Command

	jsr 	r6, DelayLong

	mov 	r0, 0x08		// Display off command
	jsr 	r6, LCD_Command

	jsr 	r6, DelayLong

	mov 	r0, 0x01		// Clear display command
	jsr 	r6, LCD_Command

	jsr 	r6, DelayLong

	mov 	r0, 0x06		// Entry mode set (Increment) command
	jsr 	r6, LCD_Command

	jsr 	r6, DelayLong

	mov 	r0, 0x0C		// Display/Cursor/Blink on command
	jsr 	r6, LCD_Command

	jsr 	r6, DelayLong

	mov 	r0, 0x02		// Return cursor home command
	jsr 	r6, LCD_Command

	jsr 	r6, DelayLong

	mov 	r0, 0x80		// Display off command
	jsr 	r6, LCD_Command


	sub	sp, sp, 1
	ld	r0, sp, 0		// Restore r0 from stack
	sub	sp, sp, 1
	ld	r6, sp, 0		// Restore r6 from stack

	jsr 	r6, r6		// Return to Main program


//=============================================================================
// Input Params:	LCD Command hex value in r0
// Output Params:	None
//-----------------------------------------------------------------------------
// Description: 	Instructs LCD screen to modify it's operation as requested
//			by Thread X.
//=============================================================================

LCD_Command:

	st	r6, sp, 0		// Store r6 on stack
	add	sp, sp, 1		// Increment to avoid overwriting
	st	r0, sp, 0		// Store r0 on stack
	add	sp, sp, 1		// Increment to avoid overwriting


	outp 	r0, GPCout		// Output Thread request into Port E output pins

	mov 	r0, 0x00		// Ensure enable(E), and RS bits are off
	outp 	r0, GPIout
	mov 	r0, 0x06		// Set enable bit to read data
	outp 	r0, GPIout

	mov	r1, 0x032
	jsr 	r6, Delay

	mov 	r0, 0x00		// End enable pulse
	outp 	r0, GPIout


	sub	sp, sp, 1
	ld	r0, sp, 0		// Restore r0 from stack
	sub	sp, sp, 1
	ld	r6, sp, 0		// Restore r6 from stack

	jsr 	r6, r6		// return to code

//=============================================================================
// Input Params:	LCD Command hex value in r0
// Output Params:	None
//-----------------------------------------------------------------------------
// Description: 	Instructs LCD screen to modify it's operation as requested
//			by Thread X.
//=============================================================================

LCD_Command1:

	st	r6, sp, 0		// Store r6 on stack
	add	sp, sp, 1		// Increment to avoid overwriting
	st	r0, sp, 0		// Store r0 on stack
	add	sp, sp, 1		// Increment to avoid overwriting
	st  r1, sp, 0
	add sp, sp, 1

	outp 	r0, GPCout		// Output Thread request into Port E output pins

	mov 	r0, 0x00		// Ensure enable(E), and RS bits are off
	outp 	r0, GPIout
	mov 	r0, 0x02		// Set enable bit to read data
	outp 	r0, GPIout

	mov	r1, 0x032
	jsr 	r6, Delay

	mov 	r0, 0x00		// End enable pulse
	outp 	r0, GPIout

	sub sp, sp, 1
	ld  r1, sp,  0
	sub	sp, sp, 1
	ld	r0, sp, 0		// Restore r0 from stack
	sub	sp, sp, 1
	ld	r6, sp, 0		// Restore r6 from stack

	jsr 	r6, r6		// return to code

//=============================================================================
// Input Params:	LCD Command hex value in r0
// Output Params:	None
//-----------------------------------------------------------------------------
// Description: 	Instructs LCD screen to modify it's operation as requested
//			by Thread X.
//=============================================================================

LCD_Command2:

	st	r6, sp, 0		// Store r6 on stack
	add	sp, sp, 1		// Increment to avoid overwriting
	st	r0, sp, 0		// Store r0 on stack
	add	sp, sp, 1		// Increment to avoid overwriting
	st  r1, sp, 0
	add sp, sp, 1


	outp 	r0, GPCout		// Output Thread request into Port E output pins

	mov 	r0, 0x00		// Ensure enable(E), and RS bits are off
	outp 	r0, GPIout
	mov 	r0, 0x04		// Set enable bit to read data
	outp 	r0, GPIout

	mov	r1, 0x032
	jsr 	r6, Delay

	mov 	r0, 0x00		// End enable pulse
	outp 	r0, GPIout

	sub 	sp, sp, 1
	ld  	r1, sp, 0
	sub	sp, sp, 1
	ld	r0, sp, 0		// Restore r0 from stack
	sub	sp, sp, 1
	ld	r6, sp, 0		// Restore r6 from stack

	jsr 	r6, r6		// return to code





//=============================================================================
// Input Params:	ASCII character hex value in r0
// Output Params:	None
//-----------------------------------------------------------------------------
// Description: 	Instructs LCD screen to display ASCII character requested
//			by Thread X.
//=============================================================================

LCD_Char1:

	st	r6, sp, 0		// Store r6 on stack
	add	sp, sp, 1		// Increment to avoid overwriting
	st	r0, sp, 0		// Store r0 on stack
	add	sp, sp, 1		// Increment to avoid overwriting
	st  r1, sp, 0
	add sp, sp, 1

	outp 	r0, GPCout		// Output Thread request into Port E output pins

	mov 	r0, 0x01		// Ensure enable(E) off, RS bit is on
	outp	r0, GPIout
	mov 	r0, 0x03		// Set enable bit to read data
	outp 	r0, GPIout

	mov	r1, 0x14
	jsr 	r6, Delay

	mov 	r0, 0x01		// End enable pulse
	outp 	r0, GPIout


	sub sp, sp, 1
	ld  r1, sp, 0
	sub	sp, sp, 1
	ld	r0, sp, 0		// Restore r0 from stack
	sub	sp, sp, 1
	ld	r6, sp, 0		// Restore r6 from stack

	jsr 	r6, r6		// return to code

//=============================================================================
// Input Params:	ASCII character hex value in r0
// Output Params:	None
//-----------------------------------------------------------------------------
// Description: 	Instructs LCD screen to display ASCII character requested
//			by Thread X.
//=============================================================================

LCD_Char2:

	st	r6, sp, 0		// Store r6 on stack
	add	sp, sp, 1		// Increment to avoid overwriting
	st	r0, sp, 0		// Store r0 on stack
	add	sp, sp, 1		// Increment to avoid overwriting
	st  r1, sp, 0
	add sp, sp, 1

	outp 	r0, GPCout		// Output Thread request into Port E output pins

	mov 	r0, 0x01		// Ensure enable(E) off, RS bit is on
	outp	r0, GPIout
	mov 	r0, 0x05		// Set enable bit to read data
	outp 	r0, GPIout

	mov	r1, 0x14
	jsr 	r6, Delay


	mov 	r0, 0x01		// End enable pulse
	outp 	r0, GPIout

	sub sp, sp, 1
	ld  r1, sp, 0
	sub	sp, sp, 1
	ld	r0, sp, 0		// Restore r0 from stack
	sub	sp, sp, 1
	ld	r6, sp, 0		// Restore r6 from stack

	jsr 	r6, r6		// return to code



#endif
